package org.celstec.mobilegame.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.celstec.mobilegame.gameLoaders.LocatoryLoader;
import org.celstec.mobilegame.locatory.Locatory;
import org.celstec.mobilegame.locatory.LocatoryPlay;
import org.celstec.mobilegame.locatory.User;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class ManageServlet extends HttpServlet {

	// public static HashMap<String, Locatory> games = new HashMap<String,
	// Locatory>();

	public static HashMap<String, LocatoryPlay> hm = new HashMap<String, LocatoryPlay>();

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String action = req.getParameter("action");
		if (action == null){
//			req.getInputStream();
			System.out.println("wrong");
//			cr
		}
		if (action.equals("createGame")) createGame(req.getParameter("gameName"), req.getInputStream());
		if (action.equals("deletePlay")) deletePlay(req.getParameter("playName"));
		if (action.equals("createPlay")) createPlay(req.getParameter("playName"), req.getParameter("gameName"));

		
	}

	public void createGame(String game, InputStream in){
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(in);
			XMLOutputter out = new XMLOutputter();
			out.setFormat(Format.getPrettyFormat());
//			System.out.println(out.outputString(doc));
			Locatory locatory = LocatoryLoader.load(doc.getRootElement());
			locatory.name = game;
			SubmitGameServiceImpl.makeGamePersistent(locatory);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void deletePlay(String playName) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from " + LocatoryPlay.class.getName()
					+ " where name =='" + playName + "'";
			List<LocatoryPlay> plays = (List<LocatoryPlay>) pm.newQuery(query).execute();
			for (Iterator iterator = plays.iterator(); iterator.hasNext();) {
				LocatoryPlay locatoryPlay = (LocatoryPlay) iterator.next();
				Query q = pm.newQuery(User.class);
			    q.setFilter("play == pKey");
			    q.declareParameters("com.google.appengine.api.datastore.Key pKey");
				List<User> users = (List<User>) q.execute(locatoryPlay.key);
				pm.deletePersistentAll(users);
			}
			pm.deletePersistentAll(plays);

		} finally {
			pm.close();

		}
	}
	
	public void createPlay(String playName, String gameName){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			String query = "select from " + Locatory.class.getName()+" where name =='"+gameName+"'";
			List<Locatory> games = (List<Locatory>) pm.newQuery(query).execute();
			if (games.size()!=0){
				LocatoryPlay play = games.get(0).createPlay(playName);
				pm.makePersistent(play);
			}
		} finally {			
			pm.close();

		}
	}

}
